//
//  CommentView.m
//  YYPHSpeedJump
//
//  Created by speedjump on 15/12/28.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "CommentView.h"

@implementation CommentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubviews];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
    }
    return self;
}
- (void)addSubviews
{
    self.commentText = [[BaseTextField alloc]initWithFrame:CGRectMake(15*kWidthScale, 15*kHeightScale,320*kWidthScale , 80*kHeightScale)];
    
    self.commentText.layer.borderWidth = 1;
    [self.commentText.layer setBorderColor:kColors(197, 204, 207, 1).CGColor];
    self.commentText.delegate =self;
    self.commentText.font = [UIFont systemFontOfSize:12];
    self.commentText.placeholder = @" 输入评论（6~50个字符以内）";
    self.commentText.layer.cornerRadius = 5;
    [self addSubview:self.commentText];
    _returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _returnBtn.backgroundColor = [UIColor grayColor];
    _returnBtn.layer.cornerRadius = 5;
    _returnBtn.frame = CGRectMake(40*kWidthScale, 110*kHeightScale, 125*kWidthScale, 35*kHeightScale);
    [_returnBtn setTitle:@"返回" forState:UIControlStateNormal];
    _returnBtn.titleLabel.font = [UIFont systemFontOfSize:15];
   
    [self addSubview:_returnBtn];
    _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _commentBtn.frame = CGRectMake(195*kWidthScale, 110*kHeightScale, 125*kWidthScale, 35*kHeightScale);
    _commentBtn.backgroundColor = kColors(218, 166, 46, 1);
    _commentBtn.layer.cornerRadius = 5;
    [_commentBtn setTitle:@"评论" forState:UIControlStateNormal];
    _commentBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:_commentBtn];
    self.commentText.delegate = self;

}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
     if (selectedRange && pos) {
        
        NSInteger startOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.start];
        
        NSInteger endOffset = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selectedRange.end];
        
        NSRange offsetRange = NSMakeRange(startOffset, endOffset - startOffset);
        
        
        
        if (offsetRange.location < 50) {
            
            return YES;
            
        }
        
        else
            
        {
            return NO;
            
        }
        
    }
    
    
    
    
    
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    
    
    NSInteger caninputlen = 50 - comcatstr.length;
    
    
    
    if (caninputlen >= 0)
        
    {
        
        return YES;
        
    }
    
    else
        
    {
        
        NSInteger len = text.length + caninputlen;
        
        //防止当text.length + caninputlen < 0时，使得rg.length为一个非法最大正数出错
        
        NSRange rg = {0,MAX(len,0)};
        
        
        
        if (rg.length > 0)
            
        {
            
            NSString *s = [text substringWithRange:rg];
            
            
            
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
            
            //既然是超出部分截取了，哪一定是最大限制了。
            
            // self.reasonText.text = [NSString stringWithFormat:@"%d/%ld",0,(long)40];
            
        }
        
        return NO;
        
    }
    
    
}
- (void)textViewDidChange:(UITextView *)textView

{
    
    UITextRange *selectedRange = [textView markedTextRange];
    
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    
    
    
    //如果在变化中是高亮部分在变，就不要计算字符了
    
    if (selectedRange && pos) {
        
        return;
        
    }
    
    
    
    NSString  *nsTextContent = textView.text;
    
    NSInteger existTextNum = nsTextContent.length;
    
    
    if (existTextNum > 50)
        
    {
        //截取到最大位置的字符
        
        NSString *s = [nsTextContent substringToIndex:50];
        
        
        [textView setText:s];
        
    }
    //self.reasonText.text = [NSString stringWithFormat:@"%ld/%d",MAX(0,40- existTextNum),40];
    
}




@end
